Repository navigation
[BTY-62] MEDFAIR: subgroup fairness metrics, fairness-aware model selection and resampling, with a HAM10000 setup - #1560
Conversation
Test CoverageTotal: 89.13% (63783 / 71562 lines)
|
|
/review |
There was a problem hiding this comment.
Previous review
AI Review
suggestion: approve
decision: human review
Thank you for this contribution, Sanni. It brings MEDFAIR's subgroup fairness metrics, its DTO and minimax-Pareto model selection, a group-balanced sampler and dataloader, HAM10000 demographics, a malignancy task and a runnable example. The code looks correct and complete, and the description explains the deliberate deviations from MEDFAIR clearly. Every new link between components is named in the description with a reason: the shared collate helper, the reuse of ece_confidence_binary, and the task reading the dataset's new columns.
Coverage meets the bar. The whole repository is at 86.09%, against 60%. The new files are at 100% (group_fairness.py, fairness_selection.py, ham10000_malignancy.py) and 95.6% (group_sampler.py). ham10000.py is at 93.9%. pyhealth/datasets/utils.py shows 49% for the whole file, but that is existing code. The new _sample_collate_fn helper is exercised by the sampler tests.
Core part of the codebase: the change edits get_dataloader in pyhealth/datasets/utils.py, a shared utility. Its collate setup moves into a new _sample_collate_fn helper, which the new loader also uses. It looks behaviour-preserving to me. Because it changes shared code that other components rely on, it can't be approved automatically, and a maintainer will want to take a look.
Optional suggestions:
pyhealth.samplernow imports frompyhealth.datasets. This sits in an import cycle withdatasets/imaging/patches.pythat works only because of import order. A lazy import, or putting the helper somewhere neutral, would make it sturdier.- In
docs/api/tasks/pyhealth.tasks.HAM10000MalignancyClassification.rstthe title underline is shorter than the title, which makes Sphinx warn. Please extend it. - The MEDFAIR figures quoted in the example docstring are good to keep, but a short note on where in the paper they come from would help readers.
Reviewed commit adf81e6
172436c to
1b48869
Compare
|
Thanks for the review. Pushed 79ce294 and afc7260, rebased on current master. Replied inline on each point; summary:
Since the shared-code change is gone, could this go back to |
1b48869 to
afc7260
Compare
|
/review Generated by Claude Code |
There was a problem hiding this comment.
Previous review
AI Review
suggestion: approve
decision: human review
Thank you for this contribution, Sanni. It brings MEDFAIR's subgroup fairness metrics, its two fairness-aware selection rules, subgroup resampling and a HAM10000 malignancy setup into PyHealth. It looks ready to merge.
What I checked:
- Sharing: The description names the one link between components, which is reusing
ece_confidence_binary. It also explains whygroup_fairness_metrics_fnis separate fromsubgroup_metrics. TheHAM10000Datasetdemographics change clearly exists to feed the new task. - Coverage: The whole repository is at 86.21%, against a 60% bar. Every changed file is above 80%:
ham10000.py93.85%,fairness_selection.py100%,group_fairness.py100%,group_sampler.py95.89% andham10000_malignancy.py100%. - Docs and tests: The docs match the code, and the tests cover each new function and the demographics path.
- Scope: The
__init__.pyedits only add exports.
Optional suggestions:
- The description says "Without the file nothing changes." The source key now always includes a demographics field, so existing users get a one-time metadata rebuild. A short note in the docs would cover this.
- The tab-separated-file error in
_demographicslooks unreachable when the Dataverse CSV is the only metadata source, because_lesion_idsfails first. You could check for this earlier. get_group_balanced_dataloaderis described as a drop-in forget_dataloader, but it takes noshuffleargument. You could say so in the docstring.tpr_at_tnr80linearly interpolates the ROC curve at FPR 0.2. One sentence in the docstring would make that clear.
Reviewed commit afc7260
afc7260 to
ebeb3d3
Compare
|
Addressed the four suggestions from the latest review in ebeb3d3 and replied inline. Two I handled differently from the suggestion, with the reasons in the threads: the one-time metadata rebuild stays (old caches lack the new columns and would otherwise fail to load), and |
|
/review Generated by Claude Code |
There was a problem hiding this comment.
Previous review
AI Review
suggestion: approve
decision: human review
Thank you for this contribution. It brings MEDFAIR's evaluation and model-selection pieces to PyHealth: subgroup fairness metrics, DTO and minimax-Pareto selection, a group-balanced sampler and dataloader, and a HAM10000 malignancy task with sex and age attributes. It also adds a runnable ERM-vs-resampling example. The description is clear about the deviations from MEDFAIR. It also explains why group_fairness_metrics_fn is separate from subgroup_metrics and why ece_confidence_binary is reused.
It looks ready to merge. The new modules are self-contained, and the __init__ edits only register exports. Coverage meets the bar: the whole repo is at 86.25% (bar 60%). Every changed file is above 80%: ham10000.py 94%, group_sampler.py 96%, and the other new files 100%.
Optional suggestions:
HAM10000Datasetnow raises aValueErrorfor a tab-separatedHAM10000_metadata.csveven when the lesion groupings CSV is present. Before, that file was ignored in this case. The error message is helpful and the behavior is tested. A one-line note in the description or the dataset docstring would help anyone who already has such a file inroot.- I couldn't check the MEDFAIR Table A9 numbers quoted in the example docstring against the paper. Please double-check them.
- There is a stray long line in the
get_group_balanced_dataloaderdocstring ("Give the groups either as a sample field…"). Re-wrapping it would tidy it.
Reviewed commit ebeb3d3
ebeb3d3 to
83af6f5
Compare
|
/review |
There was a problem hiding this comment.
Previous review
AI Review
suggestion: approve
decision: human review
Thank you for this contribution. It brings MEDFAIR's subgroup fairness metrics, its two fairness-aware selection rules, a group-balanced resampler and a HAM10000 malignancy task with age and sex attributes into PyHealth. It also adds a runnable example.
It looks ready to merge. The code matches the description. The new components stay self-contained: the only link is ECE reuse, which the description names. The new metrics, sampler, task and selection files have 100% test coverage. The modified ham10000.py has 94.5% and the whole repository has 87.1%, so all clear the bar. The docs match the code, and the HAM10000 changes are described, including the one-time metadata rebuild.
Optional suggestions:
- The example docstring cites specific MEDFAIR table numbers (A8, A9, A2). Double-check them against the paper, or drop the numbers if you're unsure.
- The
HAM10000Datasetdocstring could say in one line that theage,sexandlocalizationcolumns are new, for users who read only that page.
Reviewed commit fe36979
solarsys
left a comment
There was a problem hiding this comment.
Approved by a maintainer. The AI review suggests approve on the current commit, and the build passes.
|
Approved by a maintainer, and ready to merge once it no longer conflicts with master. Recent merges touched the same registration lines in |
Port the evaluation side of MEDFAIR (Zong et al., ICLR 2023) and its resampling baseline, with HAM10000 as the first benchmark: - group_fairness_metrics_fn: overall and per-subgroup AUC, accuracy, ECE, BCE, FPR/FNR, TPR@80%TNR, worst-group AUC, AUC gap, EqOdd - dto_select / minimax_pareto_select for fairness-aware model selection - GroupBalancedSampler and get_group_balanced_dataloader - HAM10000Dataset reads age/sex/site from the Dataverse metadata - HAM10000MalignancyClassification (MEL+AKIEC vs rest, sex/age fields) - examples/medfair_ham10000.py: ERM vs resampling under three selection rules
Build the collate function inside the sampler module and import the padding helper lazily, so pyhealth.sampler no longer imports pyhealth.datasets at load time (datasets.imaging imports the sampler package). Also fix the task page underline and cite the exact MEDFAIR table quoted in the example.
Put the example next to the other HAM10000 scripts, note that it reads each split's groups in a single pass, and rename the seed/epoch test after what it checks.
Read and check HAM10000_metadata.csv in one place, so a tab-separated download gets the format hint whether or not the groupings CSV is present. Document the one-time metadata rebuild, how tpr_at_tnr80 is interpolated, and how the group-balanced loader differs from get_dataloader.
…present The file is only required when it stands in for the lesion groupings, so keep the error there and fall back to empty age/sex otherwise. Also note the Table A8 numbers and MEDFAIR's age grouping in the example.
Also name the exact MEDFAIR table rows the example quotes.
…00 code Reject non-binary labels and out-of-range probabilities, non-positive DTO maxima, missing labels, non-positive num_samples, order arguments to the group loader and fractional age bins. Warn about duplicate or missing images in the Dataverse metadata, and stop the example early when no group can be selected on.
… task dataset The task warns once when it skips images without age or sex, naming the missing Dataverse file. The example checks for that file up front and no longer fails with a KeyError when a group has no val samples.
Skip F1 tuning when no requested metric needs a threshold. Draw the resampled indices with numpy, which has no 2**24 category limit, and reject a negative seed up front. Reword the task's skip warning so it fits a split that the Dataverse file does not cover.
fe36979 to
5f1c85a
Compare
|
/review |
|
Rebased on master and kept both sides in /review |
There was a problem hiding this comment.
AI Review
suggestion: approve
decision: human review
Thank you for this contribution, Sanni. It brings MEDFAIR's subgroup fairness metrics, its two fairness-aware selection rules and subgroup resampling into PyHealth. It also adds a HAM10000 malignancy task with sex and age attributes, plus an example script.
It looks ready to merge.
- Correctness: The code matches the description. The new components are self-contained: they add exports, and they change no base class, Trainer or shared utility. The doctests and test expectations look consistent with the code.
- Sharing: The links to existing code are named in the description with reasons. That covers reusing
ece_confidence_binaryand whysubgroup_metricsis left unchanged. The sampler's use of the collate function is covered by the description's note about the random-access view that matchesget_dataloaderbatches. - Dataset source: The HAM10000 dataset already existed, and the paper link is given.
- Docs: The docs match the code.
- Coverage: It meets the bar. The whole repository is at 89.13%, against 60% required. Every changed file is at least 94%, against 80% required.
Optional suggestions:
- The example cites specific MEDFAIR appendix tables (A8, A9 and A2). Please double-check those table numbers, since they can't be verified from the repository.
- The example script has no automated test. Its synthetic smoke mode could be run in CI if you want that.
Reviewed commit 5f1c85a
solarsys
left a comment
There was a problem hiding this comment.
Approved by a maintainer. The AI review suggests approve on the current commit, and the build passes.
Contributor: Sanni Heruwala (@sanniheruwala)
Contribution type: Metrics + sampler + task (bounty BTY-62, MEDFAIR imaging fairness benchmark)
Paper: Zong, Yang and Hospedales. MEDFAIR: Benchmarking Fairness for Medical Imaging. ICLR 2023. https://arxiv.org/abs/2210.01725
What this adds
MEDFAIR's main finding is that how you pick the final model matters about as much as which debiasing method you use. So this PR brings over its evaluation and model-selection pieces plus its strongest simple baseline, and wires them to HAM10000, one of the datasets MEDFAIR benchmarks.
group_fairness_metrics_fn: overall and per-subgroup AUC, accuracy, ECE, BCE, FPR/FNR, TPR at 80% TNR, worst-group AUC, AUC gap and equalized odds. The threshold is either tuned for max F1 as in MEDFAIR, or passed in (tune on val, apply to test). ECE reusesece_confidence_binary.It is a separate function rather than an extension of
subgroup_metrics: that one wrapsbinary_metrics_fnper group at its fixed threshold, withholds small cells and returns a nested per-attribute dict. MEDFAIR needs FPR/FNR, EqOdd and TPR at 80% TNR at a tuned operating point, plus flatmetric_groupkeys that the selection functions and result tables read directly. Adding that tosubgroup_metricswould change its signature and return shape for existing callers.dto_selectandminimax_pareto_select: MEDFAIR's two fairness-aware selection rules over per-group validation AUC. The minimax rule takes an optional fixed reference group, which is how MEDFAIR applies it across methods.GroupBalancedSampler/get_group_balanced_dataloader: subgroup (or subgroup x class) resampling. PyHealth sample datasets are iterable, so the loader puts a random-access view in front of them; batches look the same as fromget_dataloader. Groups can be given directly to avoid decoding every image once. Draws depend only on (seed, epoch), so runs with different seeds don't replay each other.HAM10000Datasetreads age, sex and body site from the DataverseHAM10000_metadata.csvwhen it is inroot. That file lists the training images only, so the ISIC 2018 validation and test splits have no demographics; the task is meant for a split of the training set, as in MEDFAIR. Without the file those attributes are empty. A tab-separated copy (the default Dataverse download) is ignored with a warning, and only raises, with a hint, when it has to stand in for a missing groupings CSV. Metadata cached before this change lacks the new columns and is rebuilt once; editing the Dataverse file also triggers a rebuild.HAM10000MalignancyClassification: MEL + AKIEC vs. the rest, rows without age/sex dropped (9,948 images, as in the paper), andsex,age,age_groupon every sample. Age bins include their upper edge: the default(60,)is MEDFAIR's binary attribute, and(19, 39, 59, 79)its five-group one.Deviations from the MEDFAIR code, on purpose
Files to review
pyhealth/metrics/group_fairness.py,pyhealth/metrics/fairness_selection.pypyhealth/sampler/group_sampler.pypyhealth/tasks/ham10000_malignancy.py,pyhealth/datasets/ham10000.py(+ yaml)examples/derm/medfair_ham10000.py: ERM vs. resampling over an lr x seed grid. It picks a learning rate per selection rule from seed-averaged val scores (minimax judged on the ERM-worst group, as in MEDFAIR) and prints test mean (std). It runs as a synthetic smoke test with no arguments, or on the real data with--root.tests/core/test_group_fairness_metrics.py,test_fairness_selection.py,test_group_sampler.py,test_ham10000_malignancy_task.py. Synthetic data, about 15 s; the existing HAM10000 tests still pass.docs/api/metrics,docs/api/sampler,docs/api/tasks, and the three index files.Next: a follow-up with GroupDRO, and the same setup for PAD-UFES-20 (which has Fitzpatrick skin type) and ISIC 2019.